﻿using Digitalmes.Domain.Entities.Mes.Masters;

namespace Digitalmes.Application.Mes.Masters.Lines.Commands;

/// <summary>
/// 创建产生命令。
/// </summary>
public record CreateLineCommand(MdLineDto Line) : IRequest<IResult<bool>>;

internal sealed class CreateLineCommandHandler(IRepository<MdLine> repo) : IRequestHandler<CreateLineCommand, IResult<bool>>
{
    public async Task<IResult<bool>> Handle(CreateLineCommand request, CancellationToken cancellationToken)
    {
        if (await repo.IsAnyAsync(m => m.LineCode == request.Line.LineCode, cancellationToken))
        {
            return Result.Failure("生产线编号已存在");
        }

        var line = request.Line.Adapt<MdLine>();
        var ok = await repo.InsertAsync(line, cancellationToken);
        return Result.From(ok);
    }
}
